package org.csu.code.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.csu.code.api.common.CommonResponse;
import org.csu.code.api.entity.Farm;
import org.csu.code.api.entity.Order;
import org.csu.code.api.persistence.FarmMapper;
import org.csu.code.api.service.FarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("farmService")
public class FarmServiceImpl implements FarmService {
    @Autowired
    private FarmMapper farmMapper;

    @Override
    public CommonResponse<Farm> getFarmByFarmId(int farmId) {
        Farm farm = farmMapper.selectById(farmId);
        if(farm==null){
            return CommonResponse.createForError("没有该农场号的农场信息");
        }
        return CommonResponse.createForSuccess(farm);
    }

    @Override
    public CommonResponse<Farm> getFarmByUserId(int userId) {
        QueryWrapper<Farm> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("userid", userId);
        Farm farm = farmMapper.selectOne(queryWrapper);
        if(farm==null){
            return CommonResponse.createForError("该用户暂未拥有农场");
        }
        return CommonResponse.createForSuccess(farm);
    }

    @Override
    public CommonResponse<List<Farm>> getAllFarms() {
        List<Farm> farmList = farmMapper.selectList(null);
        if(farmList.isEmpty()){
            return CommonResponse.createForError("没有农场信息。");
        }
        return CommonResponse.createForSuccess(farmList);
    }
}
